package com.example.universitylibraryback.controller;

import com.example.universitylibraryback.entity.Book;
import com.example.universitylibraryback.service.BookService;
import com.example.universitylibraryback.dto.BookDTO;
import com.example.universitylibraryback.dto.ResultDTO;
import org.springframework.beans.BeanUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;

@Tag(name = "图书管理", description = "图书的增删改查等操作")
@RestController
@RequestMapping("/api/books")
public class BookController {
    
    @Autowired
    private BookService bookService;
    
    @Operation(summary = "获取所有图书", description = "查询系统中所有的图书信息")
    @GetMapping
    public ResultDTO<List<BookDTO>> findAll() {
        List<Book> books = bookService.findAllBooks();
        List<BookDTO> bookDTOs = new ArrayList<>();
        for (Book book : books) {
            BookDTO dto = new BookDTO();
            BeanUtils.copyProperties(book, dto);
            bookDTOs.add(dto);
        }
        return ResultDTO.success(bookDTOs);
    }
    
    @Operation(summary = "根据ID获取图书", description = "根据图书ID查询图书详情")
    @GetMapping("/{id}")
    public ResultDTO<BookDTO> findById(@Parameter(description = "图书ID") @PathVariable("id") String bookId) {
        Book book = bookService.findBookById(bookId);
        if (book == null) {
            return ResultDTO.notFound("图书不存在");
        }
        BookDTO dto = new BookDTO();
        BeanUtils.copyProperties(book, dto);
        return ResultDTO.success(dto);
    }
    
    @Operation(summary = "根据ISBN获取图书", description = "根据图书ISBN查询图书详情")
    @GetMapping("/isbn/{isbn}")
    public ResultDTO<BookDTO> findByIsbn(@Parameter(description = "图书ISBN") @PathVariable("isbn") String isbn) {
        Book book = bookService.findBookByIsbn(isbn);
        if (book == null) {
            return ResultDTO.notFound("图书不存在");
        }
        BookDTO dto = new BookDTO();
        BeanUtils.copyProperties(book, dto);
        return ResultDTO.success(dto);
    }
    
    @Operation(summary = "根据书名搜索图书", description = "根据书名关键词搜索图书")
    @GetMapping("/search/title/{title}")
    public ResultDTO<List<BookDTO>> searchByTitle(@Parameter(description = "书名关键词") @PathVariable("title") String title) {
        List<Book> books = bookService.searchBooksByTitle(title);
        List<BookDTO> bookDTOs = new ArrayList<>();
        for (Book book : books) {
            BookDTO dto = new BookDTO();
            BeanUtils.copyProperties(book, dto);
            bookDTOs.add(dto);
        }
        return ResultDTO.success(bookDTOs);
    }
    
    @Operation(summary = "根据作者搜索图书", description = "根据作者名搜索图书")
    @GetMapping("/search/author/{author}")
    public ResultDTO<List<BookDTO>> searchByAuthor(@Parameter(description = "作者名") @PathVariable("author") String author) {
        List<Book> books = bookService.searchBooksByAuthor(author);
        List<BookDTO> bookDTOs = new ArrayList<>();
        for (Book book : books) {
            BookDTO dto = new BookDTO();
            BeanUtils.copyProperties(book, dto);
            bookDTOs.add(dto);
        }
        return ResultDTO.success(bookDTOs);
    }
    
    @Operation(summary = "获取可借图书", description = "查询当前可借阅的图书列表")
    @GetMapping("/available")
    public ResultDTO<List<BookDTO>> findAvailableBooks() {
        List<Book> books = bookService.findAvailableBooks();
        List<BookDTO> bookDTOs = new ArrayList<>();
        for (Book book : books) {
            BookDTO dto = new BookDTO();
            BeanUtils.copyProperties(book, dto);
            bookDTOs.add(dto);
        }
        return ResultDTO.success(bookDTOs);
    }
    
    // 修复addBook方法 - 移除多余的return语句
    @Operation(summary = "添加图书", description = "新增一本图书到系统")
    @PostMapping
    public ResultDTO<Boolean> addBook(@Parameter(description = "图书信息") @RequestBody Book book) {
        boolean result = bookService.addBook(book);
        return result ? ResultDTO.success(true) : ResultDTO.fail(500, "添加失败");
    }
    
    // 修复updateBook方法
    @Operation(summary = "更新图书", description = "更新图书的详细信息")
    @PutMapping
    public ResultDTO<Boolean> updateBook(@Parameter(description = "更新后的图书信息") @RequestBody Book book) {
        boolean result = bookService.updateBook(book);
        return result ? ResultDTO.success(true) : ResultDTO.fail(500, "更新失败");
    }
    
    // 修复deleteBook方法
    @Operation(summary = "删除图书", description = "从系统中删除指定ID的图书")
    @DeleteMapping("/{id}")
    public ResultDTO<Boolean> deleteBook(@Parameter(description = "图书ID") @PathVariable("id") String bookId) {
        boolean result = bookService.deleteBook(bookId);
        return result ? ResultDTO.success(true) : ResultDTO.fail(500, "删除失败");
    }
    
    // 修复updateBookStatus方法
    @Operation(summary = "更新图书状态", description = "更新图书的借阅状态")
    @PutMapping("/{id}/status")
    public ResultDTO<Boolean> updateBookStatus(@Parameter(description = "图书ID") @PathVariable("id") String bookId, 
                                            @Parameter(description = "图书状态") @RequestParam("status") String status) {
        boolean result = bookService.updateBookStatus(bookId, status);
        return result ? ResultDTO.success(true) : ResultDTO.fail(500, "更新状态失败");
    }
}